#include <stdio.h>
#include "IOT.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "string.h"
#include "GNSS.h"
#include "Sensor.h"
extern gps_t *gps ;

#define NB_UART_NUM UART_NUM_2
#define NB_RXD (GPIO_NUM_19)
#define NB_TXD (GPIO_NUM_21)
#define BUF_SIZE1 (1024)
#define RX1_BUF_SIZE 		(1024)
#define TX1_BUF_SIZE 		(512)
static const char *TAG = "UART2";





char NB_CONFIG[60] = "AT+MQTTCFG=\"47.99.160.47\",12345,\"SIM\",30,\"\",\"\",1\r\n";		//配置MQTT连接参数
char NB_OPEN[30] = "AT+MQTTOPEN=0,0,0,0,1,\"\",\"\"\r\n";									//连接MQTT服务器
char NB_DIS_CONNECT[14] = "AT+MQTTDISC\r\n";						                        //MQTT断开连接
char NB_DELETE_INF[13] = "AT+MQTTDEL\r\n";							                        //MQTT删除配置信息
char NB_START[14] = "AT+MQTTSTAT?\r\n";													    //查询MQTT状态
char NB_SUB[28] = "AT+MQTTSUB=\"Tempuature\",1\r\n";										//订阅 Topic
char NB_U_SUB[28] = "AT+MQTTUNSUB=\"Tempuature\"\r\n";										//取消订阅Topic
char NB_SUB_QUERY[13] = "AT+MQTTSUB?\r\n";												    //查询已订阅Topic


/*  发布消息要拼凑的字符串  */

char NB_PUB[35] = "AT+MQTTPUB=\"Tempuature\",2,1,0,25,\"";						            //发布消息，最后一个是字节数
char Edge[4] = "\"\r\n";

char NB_Send_Data[120] ;


extern int16_t Sensor_Buffer[48];               //传感器数据
#define LINE_LEN                25              //数据长度
static uint8_t temp_buff[LINE_LEN];             //转成16进制的数组
static char buffer[25];                         //转成字符串的数组



    /*  MN316的串口初始化   */

void Uart2_NB_init(void)
{
    //串口配置结构体
    uart_config_t uart_config = 
    {
        .baud_rate = 9600,                      //波特率
        .data_bits = UART_DATA_8_BITS,         //数据位
        .parity = UART_PARITY_DISABLE,         //校验位
        .stop_bits = UART_STOP_BITS_1,         //停止位
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, //流控位
        .source_clk = UART_SCLK_APB,
    };

    //设置串口
    uart_param_config(NB_UART_NUM, &uart_config);
    //IO映射->TX->GPIO_NUM_4,RX->GPIO_NUM_5
    uart_set_pin(NB_UART_NUM, NB_TXD, NB_RXD, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    //注册串口服务即使能+设置缓存区大小
    uart_driver_install(NB_UART_NUM, BUF_SIZE1 * 2, 0, 0, NULL, 0);
}



void convertUnCharToStr(char* str, unsigned char* UnChar, int ucLen)
{
	int i = 0;
	for (i = 0; i < ucLen; i++)
	{
		//格式化输str,每unsigned char 转换字符占两位置%x写输%X写输
		sprintf(str + i * 2, "%02x", UnChar[i]);
	}
}


void Static_Data_Process(void)
{



    temp_buff[0] = 0XC5;    //帧头

    temp_buff[1] = 0XC6;    //设备号功能字

    temp_buff[2] = 0X00;    //设备号高8位
    temp_buff[3] = 0X01;    //设备号低8位

    temp_buff[4] = 0XC9;    //经度正负标志 

    temp_buff[5] = 0X22;    //经度-度值

    temp_buff[6] = 0XCB;    //经度-分功能字
    temp_buff[7] = 0X23;    //经度-分值

    temp_buff[8] = 0XCC;    //经度-秒功能字
    temp_buff[9] = 0X24;    //经度-秒值


    temp_buff[10] = 0XCD;    //纬度正负标志

    temp_buff[11] = 0X25;    //纬度-度值

    temp_buff[12] = 0XCF;    //纬度-分功能字
    temp_buff[13] = 0X26;    //纬度-分值

    temp_buff[14] = 0XD1;    //纬度-秒功能字
    temp_buff[15] = 0X27;    //纬度-秒值

    temp_buff[16] = 0XD4;    //1号温湿度传感器功能字
    temp_buff[17] = 0XD2;    //1号传感器温度正负
    temp_buff[18] = 0X28;    //1号传感器温度值
    temp_buff[19] = 0X29;    //1号传感器湿度值


    temp_buff[20] = 0XD5;    //2号温湿度传感器功能字
    temp_buff[21] = 0XD2;    //2号传感器温度正负
    temp_buff[22] = 0X28;    //2号传感器温度值
    temp_buff[23] = 0X29;    //2号传感器湿度值


    temp_buff[24] = 0XEC;    //2号传感器湿度值

}

void Process_MQTT_Data(void)
{

    float txt_1;
	uint8_t txt_2, txt_3, txt_5;
	int32_t txt_4;

    Static_Data_Process();          //静态协议帧处理 
    txt_1 = 115.5712;

    printf("经度= %.4f      ",txt_1);
	txt_2 = txt_1;
    printf("前两位= %d      ",txt_2);
	txt_3 = (txt_1 - (txt_2)) * 100 ;
    printf("中间两两位= %d      ",txt_3);
	txt_4= txt_1 * 10000;
	txt_4 = txt_4 - (txt_2 * 10000 + txt_3 * 100);
	txt_5 = txt_4;
    printf("最后两位= %d      ",txt_5);

    temp_buff[5] = txt_2 & 0xFF;    //经度-度值
    temp_buff[7] = txt_3 & 0xFF;    //经度-分值
    temp_buff[9] = txt_5 & 0xFF;    //经度-秒值



    txt_1 = 38.8879;

    printf("纬度= %.4f      ",txt_1);
	txt_2 = txt_1;
    printf("前两位= %d      ",txt_2);
	txt_3 = (txt_1 - (txt_2)) * 100 ;
	txt_4= txt_1 * 10000;
    printf("中间两两位= %d      ",txt_3);    
	txt_4 = txt_4 - (txt_2 * 10000 + txt_3 * 100);
	txt_5 = txt_4;
    printf("最后两位= %d      ",txt_5);

    temp_buff[11] =  txt_2 & 0xFF;    //纬度-度值
    temp_buff[13] =  txt_3 & 0xFF;    //纬度-分值
    temp_buff[15] =  txt_5 & 0xFF;    //纬度-秒值


	temp_buff[18] = Sensor_Buffer[0] & 0xFF;         //低八位是功能自对应的数据
	temp_buff[19] = Sensor_Buffer[1] & 0xFF;         //低八位是功能自对应的数据

	temp_buff[22] = Sensor_Buffer[2] & 0xFF;         //低八位是功能自对应的数据
	temp_buff[23] = Sensor_Buffer[3] & 0xFF;         //低八位是功能自对应的数据

    convertUnCharToStr(buffer, temp_buff, 25);       //将16进制转到字符串


    strcpy(NB_Send_Data,NB_PUB);
    //printf("temp1=%s\n",NB_Send_Data);
    strcat(NB_Send_Data, buffer);                    //按照AT指令拼凑字符串
    //printf("temp2=%s\n",NB_Send_Data);
	strcat(NB_Send_Data, Edge);                      //按照AT指令拼凑字符串
    //printf("temp3=%s\n",NB_Send_Data);

}


/*  每60S需要配置一次 MQTT服务器  */
void MQTT_Config(void)
{

    uart_write_bytes(NB_UART_NUM, NB_CONFIG, strlen(NB_CONFIG));    //发送 MQTT 配置信息
    vTaskDelay(1500 / portTICK_PERIOD_MS);

    uart_write_bytes(NB_UART_NUM, NB_OPEN, strlen(NB_OPEN));        //连接MQTT服务器
    vTaskDelay(500 / portTICK_PERIOD_MS);

    uart_write_bytes(NB_UART_NUM, NB_SUB, strlen(NB_SUB));          //订阅目标Topic

}


void MQTT_Send_Data(void)
{

    uart_write_bytes(NB_UART_NUM, NB_Send_Data, strlen(NB_Send_Data));          //对指定Topic发送信息
    vTaskDelay(500 / portTICK_PERIOD_MS);

}

void MQTT_Dis_Connected(void)
{

    vTaskDelay(5000 / portTICK_PERIOD_MS);                                 //一分钟后断开服务器
    uart_write_bytes(NB_UART_NUM, NB_DIS_CONNECT, strlen(NB_DIS_CONNECT));


    vTaskDelay(1500 / portTICK_PERIOD_MS);                                  //1.5s后发送删除配置信息
    uart_write_bytes(NB_UART_NUM, NB_DELETE_INF, strlen(NB_DELETE_INF));

}